package com.net

import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson
import com.google.gson.JsonElement
import com.helloandroid.tools.MyLog
import com.helloandroid.vo.User
import org.junit.Test

fun log(vararg msg: String) {
    msg.forEach(::print)
    println()
}

class TestLogin {

    @Test
    fun parse_user() {
        val str = """
            {"err":0,"msg":"ok","data":{"user_id":120,"coin":0,"money":0,"tx_count":0,"tx_time_stamp":null,"bs":0,"bs_target":6000,"signed_count":0,"sign_time_stamp":null,"wx_id":null,"wx_name":null,"wx_sex":0,"wx_icon":null},"serverTime":1613987081790}
        """.trimIndent()

        val jsonObj = (Gson().fromJson(str) as JsonElement).asJsonObject
        val err = jsonObj.get("err").asInt
        if (err == 0) {

            // 判断是否有json
            val hasSubJson = jsonObj.get("json")
            if (hasSubJson != null) {
                log("has sub json")
            } else {
                log("do not has sub json!")
                val str = Gson().fromJson<User>(jsonObj.get("data").toString())
                log("str:$str")
            }
        } else {
            log("err:$err")
        }
    }
}
